home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / COMMON.ZIP / KEY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-27  |  3.9 KB  |  175 lines

  1. /*
  2.     key.c
  3.     Internet: alexad3@icebox.iceonline.com
  4.     Copyright 1995, May 14 by Alec Russell, ALL rights reserved
  5.     Permission granted to use ths code as you wish.
  6.  
  7.     Created - 1995/5/14
  8.  
  9.     History:
  10.         New file
  11.  
  12.    An example of a reading raw scan codes
  13.  
  14. */
  15. #include <stdio.h>
  16. #include <dos.h>
  17.  
  18. #define BYTE unsigned char
  19. #define NUM_SCAN_QUE 256     // this MUST be 256, using BYTE roll-over for
  20.                              // q code
  21.  
  22. static void interrupt (far *oldkb)();   /* BIOS keyboard handler */
  23.  
  24. // Q code
  25. BYTE gb_scan;
  26. BYTE gb_scan_q[NUM_SCAN_QUE];
  27. BYTE gb_scan_head;
  28. BYTE gb_scan_tail;
  29.  
  30.  
  31. /*
  32.  
  33.    invoked by the hardware keyboard interupt
  34.  
  35.  
  36.    ques up the raw scan codes
  37.  
  38.    updated March 1995, to handle 0e0h better
  39.  
  40.  
  41.    stuff raw scan codes into the array gb_scan_q[]
  42. */
  43. /* ---------------------- get_scan() --------------------- April 17,1993 */
  44. void interrupt get_scan(void)
  45. {
  46.  
  47.    /* read the raw scan code from the keyboard */
  48.    asm   cli
  49.  
  50. read1:
  51.    asm   {
  52.  
  53.          in    al, 060h       /* read scan code */
  54.          cmp   al, 0e0h       /* its the second keypad */
  55.          jne    read2
  56.  
  57.          /* 0e0h indicates the second cursor pad, reset and try again */
  58.          in    al, 061h       /* read keyboard status */
  59.          mov   bl, al
  60.          or    al, 080h
  61.          out   061h, al       /* set bit 7 and write */
  62.          mov   al, bl
  63.          out   061h, al       /* write agian, bit 7 clear */
  64.  
  65.          mov   al, 020h       /* reset PIC */
  66.          out   020h, al
  67.  
  68.          jmp short read1
  69.          }
  70.  
  71. read2:
  72.    asm   {
  73.          mov   gb_scan, al
  74.  
  75.          /* this code re-sets the keyboard, and pic to get ready
  76.             for next key-press
  77.          */
  78.          in    al, 061h       /* read keyboard status */
  79.          mov   bl, al
  80.          or    al, 080h
  81.          out   061h, al       /* set bit 7 and write */
  82.          mov   al, bl
  83.          out   061h, al       /* write agian, bit 7 clear */
  84.  
  85.          mov   al, 020h       /* reset PIC */
  86.          out   020h, al
  87.  
  88.          /* end of re-set code */
  89.  
  90.          sti
  91.          }
  92.  
  93.    *(gb_scan_q+gb_scan_tail)=gb_scan;
  94.    ++gb_scan_tail;
  95.  
  96.    /*
  97.  
  98.    If you just want to look for certain keys for some special
  99.    action, but want the keyboard to otherwise behave normally
  100.    don't call the re-set code, and DO call the old keyboard handler
  101.  
  102.    simply like this:
  103.  
  104.  
  105.    oldkb();
  106.  
  107.    If you don't call oldkb() you MUST have that re-set code in there
  108.    or your computer will hang or crash.
  109.  
  110.    Now that you have the raw scan code it is up to you to convert
  111.    it to ASCII or do whatever you want.
  112.  
  113.    */
  114. }
  115.  
  116. /* ---------------------- init_keyboard() ---------------- April 17,1993 */
  117. void init_keyboard(void)
  118. {
  119.    BYTE far *bios_key_state;
  120.  
  121.    /* save old BIOS key board handler */
  122.    oldkb=getvect(9);
  123.  
  124.    // turn off num-lock via BIOS 
  125.    bios_key_state=MK_FP(0x040, 0x017);
  126.    *bios_key_state&=(~(32 | 64));     // toggle off caps lock and
  127.                                       // num lock bits in the BIOS varible
  128.    oldkb();      // call BIOS keyhandler to change keyboard lights
  129.  
  130.    gb_scan_head=0;
  131.    gb_scan_tail=0;
  132.    gb_scan=0;
  133.  
  134.    /* setup our own handler */
  135.    setvect(9, get_scan);
  136.  
  137. }
  138.  
  139. /* restore the bios keyboard handler */
  140. /* ---------------------- deinit_keyboard() -------------- April 17,1993 */
  141. void deinit_keyboard(void)
  142. {
  143.    setvect(9, oldkb);
  144. }
  145.  
  146.  
  147. /* ---------------------- main() ------------------------ October 7,1995 */
  148. void main(void)
  149. {
  150.    int key;
  151.  
  152.    init_keyboard();
  153.  
  154.    printf("Press ESC to quit\n");
  155.    key=999;
  156.    do
  157.       {
  158.       while ( gb_scan_head != gb_scan_tail )
  159.          {
  160.          key=*(gb_scan_q+gb_scan_head);
  161.          ++gb_scan_head;
  162.          
  163.          printf("%03d %03x\n", key, key);
  164.          }
  165.  
  166.       }
  167.    while ( key != 1 );
  168.  
  169.    deinit_keyboard();
  170. }
  171.  
  172.  
  173.  
  174. /* ------------------------------ EOF -------------------------------- */
  175.